package com.shxy.rlzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shxy.rlzy.entity.Depart;
import com.shxy.rlzy.entity.EmployeeInfo;
import com.shxy.rlzy.entity.Salary;
import com.shxy.rlzy.mapper.DepartMapper;
import com.shxy.rlzy.mapper.EmployeeInfoMapper;
import com.shxy.rlzy.mapper.SalaryMapper;
import com.shxy.rlzy.service.ISalaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tanhao
 * @since 2022-02-16
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {

    private SalaryMapper salaryMapper;
    private EmployeeInfoMapper employeeInfoMapper;
    private DepartMapper departMapper;

    @Autowired
    public void setSalaryMapper(SalaryMapper salaryMapper) {
        this.salaryMapper = salaryMapper;
    }

    @Autowired
    public void setEmployeeInfoMapper(EmployeeInfoMapper employeeInfoMapper) {
        this.employeeInfoMapper = employeeInfoMapper;
    }

    @Autowired
    public void setDepartMapper(DepartMapper departMapper) {
        this.departMapper = departMapper;
    }

    @Override
    public List<Salary> getList(Long departId, String gender, String name, String phone) {
        // 查询员工工资信息列表
        List<Salary> salaries = salaryMapper.getList(departId, name, phone);
        if(salaries.size() > 0) {
            // 获取员工编号列表
            List<Long> empIds = salaries.stream().map(Salary::getEmployeeId).collect(Collectors.toList());
            // 获取全部员工信息
            LinkedHashMap<Long, EmployeeInfo> employeeMap = employeeInfoMapper.selectBatchIds(empIds).stream().collect(Collectors.toMap(EmployeeInfo::getId, item -> item, (oldVal, newVal) -> newVal, LinkedHashMap::new));
            // 获取全部部门信息
            LinkedHashMap<Long, Depart> departMap = departMapper.getAll().stream().collect(Collectors.toMap(Depart::getId, item -> item, (oldVal, newVal) -> newVal, LinkedHashMap::new));
            // 将工资信息中的员工信息和部门信息设置完整
            salaries.forEach(salary -> {
                EmployeeInfo emp = employeeMap.get(salary.getEmployeeId());
                emp.setDepart(departMap.get(emp.getDepartId()));
                salary.setEmployeeInfo(emp);
            });
        }
        return salaries;
    }

    @Override
    public int initBatch() {
        List<Integer> ids = salaryMapper.getEmpIdsNotInSalary();
        System.out.println("没有工资信息的员工" + ids.toString());
        List<Salary> salaries = ids.stream().map(id -> {
            Salary salary = new Salary();
            salary.setEmployeeId(id);
            return salary;
        }).collect(Collectors.toList());
        saveBatch(salaries);
        return salaries.size();
    }

    @Override
    public Salary getByIdCard(String idCard) {
        return salaryMapper.getByIdCard(idCard);
    }
}
